﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace OF.Utility
{
    public class CompareObject
    {
        public static string GetObjectInfo(object oldInfo, object newInfo)
        {
            StringBuilder sb = new StringBuilder();
            Type Objoldinfo = oldInfo.GetType();
            //需要改变的客户信息
            Type Objnewinfo = newInfo.GetType();

            PropertyInfo[] finfo_oldlist = Objoldinfo.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            PropertyInfo[] finfo_newlist = Objoldinfo.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            int intLength = finfo_oldlist.Length;

            for (int i = 0; i < intLength; i++)
            {
                if (finfo_oldlist[i].GetValue(oldInfo, null) != null && finfo_newlist[i].GetValue(newInfo, null) != null)
                {

                    if (!finfo_oldlist[i].GetValue(oldInfo, null).Equals(finfo_newlist[i].GetValue(newInfo, null)))
                    {
                        object[] objs = finfo_oldlist[i].GetCustomAttributes(typeof(DescriptionAttribute), true);
                        string des = "";
                        if (objs.Length > 0)
                        {
                            des = ((DescriptionAttribute)objs[0]).Description;
                        }
                        string proValue = "";
                        object objPropertyValue = finfo_newlist[i].GetValue(oldInfo, null);
                        if (objPropertyValue != null)
                        {
                            proValue = objPropertyValue.ToString();
                        }

                        string newValue = "";
                        object objnewPropertyValue = finfo_newlist[i].GetValue(newInfo, null);
                        if (objnewPropertyValue != null)
                        {
                            newValue = objnewPropertyValue.ToString();
                        }
                        if (proValue != newValue)
                        {
                            sb.AppendFormat("修改了 <strong>{0}</strong>:原数据“{1}”,修改为“{2}”<br>", des, proValue, newValue);
                        }

                    }

                }

            }
            return sb.ToString();
        }

        /// <summary>
        /// 获取实体所有数据
        /// </summary>
        /// <param name="info">实体</param>
        /// <param name="filed">取消的字段</param>
        /// <returns></returns>
        public static string GetObjectStr(object info,string[] filed)
        {
            StringBuilder sb = new StringBuilder();
            Type Objoldinfo = info.GetType();
            PropertyInfo[] finfo_oldlist = Objoldinfo.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            int intLength = finfo_oldlist.Length;
            for (int i = 0; i < intLength; i++)
            {
                if (filed!=null&&filed.Contains(finfo_oldlist[i].Name))
                {
                    continue;
                }
                if (finfo_oldlist[i].GetValue(info, null) != null)
                {
                    string proValue = "";
                    object objPropertyValue = finfo_oldlist[i].GetValue(info, null);
                    if (objPropertyValue != null)
                    {
                        proValue = objPropertyValue.ToString();
                    }
                    sb.Append(proValue);
                }
            }
            return sb.ToString();
        }
    }
}
